--------------------------------------------------------------------------------------------------------
-- 一个简单的脑子
--------------------------------------------------------------------------------------------------------
require("behaviours/follow")
require("behaviours/faceentity")
require("behaviours/chaseandattack")
require("behaviours/runaway")
require("behaviours/wander")
--------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------
local BrainCommon = require("brains/braincommon")
--------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------
-- local AVOID_EXPLOSIVE_DIST = 5
--------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------
-- -- 走位
-- local function ShouldAvoidExplosive(target)
--     return target.components.explosive == nil
-- end
--------------------------------------------------------------------------------------------------------
-- 找树砍
local function IsDeciduousTreeMonster(guy)
    return guy.monster and guy.prefab == "deciduoustree"
end

local CHOP_MUST_TAGS = { "CHOP_workable" }
local function FindDeciduousTreeMonster(inst)
    return FindEntity(inst, 10 / 3, IsDeciduousTreeMonster, CHOP_MUST_TAGS)
end

local function KeepChoppingAction(inst)
    return (inst.components.follower.leader ~= nil and
            inst:IsNear(inst.components.follower.leader, 8))
        or FindDeciduousTreeMonster(inst) ~= nil
end

local function StartChoppingCondition(inst)
    return (inst.components.follower.leader ~= nil and
            inst.components.follower.leader.sg ~= nil and
            inst.components.follower.leader.sg:HasStateTag("chopping"))
        or FindDeciduousTreeMonster(inst) ~= nil
end

local function FindTreeToChopAction(inst)
    local target = FindEntity(inst, 10, nil, CHOP_MUST_TAGS)
    if target ~= nil and KeepChoppingAction(inst) then
        target = FindDeciduousTreeMonster(inst) or target
        inst.AnimState:OverrideSymbol("swap_object", "swap_axe", "swap_axe")
        inst.AnimState:Hide("ARM_normal")
        inst.AnimState:Show("ARM_carry")
        return BufferedAction(inst, target, ACTIONS.CHOP)
    end
end
--------------------------------------------------------------------------------------------------------
-- 找矿挖
local function KeepMiningAction(inst)
    return (inst.components.follower.leader ~= nil and
        inst:IsNear(inst.components.follower.leader, 8))
end

local function StartMiningCondition(inst)
    return (inst.components.follower.leader ~= nil and
        inst.components.follower.leader.sg ~= nil and
        inst.components.follower.leader.sg:HasStateTag("mining"))
end

local MINE_MUST_TAGS = { "MINE_workable" }
local function FindThingToMineAction(inst)
    local target = FindEntity(inst, 10, nil, MINE_MUST_TAGS)
    if target ~= nil and target.prefab ~= "daywalker_pillar" and KeepMiningAction(inst) then
        inst.AnimState:OverrideSymbol("swap_object", "swap_pickaxe", "swap_pickaxe")
        inst.AnimState:Hide("ARM_normal")
        inst.AnimState:Show("ARM_carry")
        return BufferedAction(inst, target, ACTIONS.MINE)
    end
end
--------------------------------------------------------------------------------------------------------
-- 获取玩家实体
local function GetOwner(inst)
    return inst.components.follower.leader
end
--------------------------------------------------------------------------------------------------------
-- 确认是否面向
local function KeepFaceTargetFn(inst, target)
    inst.AnimState:Show("ARM_normal")
    inst.AnimState:Hide("ARM_carry")
    return GetOwner(inst) == target
end
--------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------
--  Brain
local ymh_shadow_brain = Class(Brain, function(self, inst)
    Brain._ctor(self, inst)
end)
--------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------
function ymh_shadow_brain:OnStart()
    local root = PriorityNode({ -- This protector is set to defend an ar ea and then vanish.
        -- 砍树
        IfThenDoWhileNode(function() return StartChoppingCondition(self.inst) end,
            function() return KeepChoppingAction(self.inst) end, "aragakiwi_chop",
            LoopNode { ChattyNode(self.inst, "ARAGAKIWI_HELP_CHOP",
                DoAction(self.inst, FindTreeToChopAction)) }),
        -- 挖矿
        IfThenDoWhileNode(function() return StartMiningCondition(self.inst) end,
            function() return KeepMiningAction(self.inst) end, "aragakiwi_mine",
            LoopNode { ChattyNode(self.inst, "ARAGAKIWI_HELP_MINE",
                DoAction(self.inst, FindThingToMineAction)) }),
        -- 进行攻击
        -- function(self, inst, max_chase_time[最大追击时间], give_up_dist, max_attacks[最大攻击次数], findnewtargetfn, walk, distance_from_ocean_target[？])
        ChaseAndAttack(self.inst --[[, TUNING.ARAGAKIWI_MAX_CHASE_TIME, TUNING.ARAGAKIWI_GIVEUP_DIST--]]),
        -- 优先跟随玩家，其次才是攻击，避免我们跑路他还不走
        ChattyNode(self.inst, "ARAGAKIWI_FOLLOW",
            Follow(self.inst, GetOwner, 1, 5,
                9)),

        -- 在玩家身边的时候 朝向玩家 参数 1 玩家实体 2 保持面向
        FaceEntity(self.inst, GetOwner, KeepFaceTargetFn),

        -- 在玩家身边晃悠
        Wander(self.inst),
    }, 0.25) -- 这个是刷帧的间隔？

    self.bt = BT(self.inst, root)
end

--------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------
return ymh_shadow_brain
--------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------
